package com.gizwits.opensource.appkit.yasystem;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiDeviceListener;
import com.gizwits.opensource.appkit.ControlModule.GosControlModuleBaseActivity;
import com.gizwits.opensource.appkit.R;

import java.util.concurrent.ConcurrentHashMap;

public class Main2Activity extends GosControlModuleBaseActivity {
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10,button11;
    private LocalBroadcastManager localBroadcastManager;
    String data;
    String tempdata;
    TextView tv1;
    private GizWifiDevice device;
    GosControlModuleBaseActivity go;
    ImageView imageView;
    int color = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        go = new GosControlModuleBaseActivity();
        Intent intent = getIntent();
        device = (GizWifiDevice) intent.getParcelableExtra("GizWifiDevice");
        device.setListener(mGizWifiDeviceListener);
        initView();
        initEvent();
    }
    private GizWifiDeviceListener mGizWifiDeviceListener= new GizWifiDeviceListener(){
        //接收数据回调
        @Override
        public void didReceiveData(GizWifiErrorCode result, GizWifiDevice device, ConcurrentHashMap<String, Object> dataMap, int sn) {
            super.didReceiveData(result, device, dataMap, sn);
            Log.d("接收到的数据",dataMap.toString());
            go.getDataFromReceiveDataMap(dataMap);
            updateUI();
        }
    };

    ///更新界面
    protected void updateUI() {
        tempdata = "";
        if (data_fire){
        }
        tempdata+="剩余和面时间"+data_hetime+"  ";
        tv1.setText(tempdata);
    }



    public  void initView(){
        tv1 = (TextView) findViewById(R.id.tv1);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        button1 = (Button) findViewById(R.id.btn2);
        button2 = (Button) findViewById(R.id.btn3);
        button3 = (Button) findViewById(R.id.btn4);
        button4 = (Button) findViewById(R.id.btn5);
        button5 = (Button) findViewById(R.id.btn6);
        imageView = (ImageView) findViewById(R.id.image);
        button6 = (Button) findViewById(R.id.btn7);
        button7 = (Button) findViewById(R.id.btn8);
        button8 = (Button) findViewById(R.id.btn9);
        button9 = (Button) findViewById(R.id.btn10);
        button10 = (Button) findViewById(R.id.btn11);
        button11 = (Button) findViewById(R.id.btn12);
    }

    public void initEvent(){
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
        button4.setOnClickListener(onClickListener);
        button5.setOnClickListener(onClickListener);
        button6.setOnClickListener(onClickListener);
        button7.setOnClickListener(onClickListener);
        imageView.setOnClickListener(onClickListener);
        button8.setOnClickListener(onClickListener);
        button9.setOnClickListener(onClickListener);
        button10.setOnClickListener(onClickListener);
        button11.setOnClickListener(onClickListener);
    }
    Button.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn2:
                    color = 1;
                    color(color);
                    data = "SmstartE";
                    retun(data);
                    break;
                case R.id.btn3:
                    color = 2;
                    color(color);
                    data="SmstopE";
                    retun(data);
                    break;
                case R.id.btn4:
                    color = 3;
                    color(color);
                    data="StstartE";
                    retun(data);
                    break;
                case R.id.btn5:
                    color = 4;
                    color(color);
                    data = "SstopE";
                    retun(data);
                    break;
                case R.id.btn6:
                    color = 5;
                    color(color);
                    data = "SwstartE";
                    retun(data);
                    break;
                case R.id.btn7:
                    color = 6;
                    color(color);
                    data = "SwstopE";
                    retun(data);
                    break;
                case R.id.btn8:
                    data = "Stime+E";
                    retun(data);
                    break;
                case R.id.btn9:
                    data = "Stime-E";
                    retun(data);
                    break;
                case R.id.btn10:
                    data = "Sspeed+E";
                    retun(data);
                    break;
                case R.id.btn11:
                    data = "Sspeed-E";
                    retun(data);
                    break;
                case R.id.btn12:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                case R.id.image:
                    data = "ScloseautoE";
                    retun(data);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };


    ///按钮颜色改变
    private  void color(int i){
        switch (i){
            case 1:
                button1.setBackgroundColor(Color.parseColor("#007AFE"));
                button2.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 2:
                button1.setBackgroundColor(Color.parseColor("#F8DC26"));
                button2.setBackgroundColor(Color.parseColor("#FF9500"));
                break;
            case 3:
                button3.setBackgroundColor(Color.parseColor("#FF9500"));
                button4.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 4:
                button4.setBackgroundColor(Color.parseColor("#FF9500"));
                button3.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 5:
                button5.setBackgroundColor(Color.parseColor("#FF9500"));
                button6.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;
            case 6:
                button6.setBackgroundColor(Color.parseColor("#FF9500"));
                button5.setBackgroundColor(Color.parseColor("#F8DC26"));
                break;

        }
    }

    @Override
    protected void onDestroy() {
        data = "ScloseautoE";
        retun(data);;
        super.onDestroy();
    }
    public void retun(String data){
        Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
        intent.putExtra("key",data);
        intent.putExtra("data",true);
        localBroadcastManager.sendBroadcast(intent);
    }

}
